function onLogout() {
  $.ajax({
    url: '/logout',
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        /*console.log(data);*/
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function onCheck(){
  var snum = document.getElementById('getsnum').value;
  hit_load(snum);
  document.getElementById('bingo').style.top = '80px';
}

function table_load() {
  console.log('table load');
  $.ajax({
    url: '/realtime',
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        init_all_table();
        updateTable(data);
        $('.td1').on("click",function(){
          console.log($(this).text());
          /*console.log($(this).offset());*/
          hit_load($(this).text());
          document.getElementById('bingo').style.top = $(this).offset().top+'px';
        })
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function hit_load(sname){
  $.ajax({
    url: '/hit?snum='+sname,
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        lines = data.split('\n');
        var str = '';
        for(var i in lines){
          items = lines[i].split(',')
          str+='<tr>'
          for(var j in items){
            if(j>0){
              str+='<td>'+items[j]+'</td>'
            }
          }
          str+='</tr>'
        }
        $('#hit-table').html('<tr><td>日期</td><td>模型</td><td>收益1</td><td>收益2</td><td>收益3</td><td>收益4</td><td>收益5</td></tr>');
        $('#hit-table').append(str);
        $('#hit-name').html(sname);
        document.getElementById('bingo').style.display = 'block';
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}
function res_load(sname){
  $.ajax({
    url: '/res?name='+sname,
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        return data;
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

window.onload = init();
function init(){
  document.getElementById('bingo').style.display = 'none';
  document.getElementById('bingo').style.position = 'absolute';
  document.getElementById('bingo').style.right = '10px'
  document.getElementById('bingo').style.top = '80px'
  document.getElementById('bingo').style.width = '500px'
  document.getElementById('bingo').style.backgroundColor = 'black'
  document.getElementById('bingo').style.opacity = '0.8'
  document.getElementById('bingo').style.color = 'green'
  document.getElementById('bingo').style.paddingBottom = '20px'
  init_all_table();
}

function init_all_table(){
  var iver_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '今日涨幅',
    'T+2期望预测',
    '预测成功率',
    '均线顺线',
    '成交量聚类',
    '成交量期望',
    '开高低收聚类30日',
    '开高低收聚类90日',
    '开高低收聚类300日'
  ]

  var antonio_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '今日涨幅',
    '均线顺线',
    '成交量聚类',
    '成交量期望',
    '开高低收聚类30日',
    '开高低收聚类90日',
    '开高低收聚类300日'
  ]

  var kobe_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    'T+4期望预测',
    '预测成功率',
    '成交量聚类'
  ]

  var wb_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '所在周期',
    '当前最低是否大于MA10',
    'T日前（包括）涨停板数目'
  ]

  var harden_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    'T+12开盘预测',
    '成功率',
    'T+12最高预测',
    '成功率',
    'T+12最低预测',
    '成功率',
    'T+12收盘预测',
    '成功率'
  ]

  var oneal_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '涨幅',
    '聚类标记'
  ]

  var haward_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '涨幅',
    '是否涨停板'
  ]

  var rockets_header=[
    '股票编号',
    '近期中签',
    '预测时间'
  ]

  var rockets_week_header=[
    '股票编号',
    '近期中签',
    '预测时间'
  ]

  var rockets_month_header=[
    '股票编号',
    '近期中签',
    '预测时间'
  ]

  var james_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '连涨天数',
    '期间涨幅',
    '涨停板日期',
    '今日阴线'
  ]

  var cpp_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var cpp_month_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var cppp_month_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var cppp_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停',
    '标记'
  ]

  var curry_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var curry_week_header=[
    '股票编号',
    '近期中签',
    '预测时间'
  ]

  var curry_month_header=[
    '股票编号',
    '近期中签',
    '预测时间'
  ]

  var lv_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '周期',
    '近15天涨停'
  ]

  var durant_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var yao_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var rondo_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var rondop_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]
  init_table('#iver-table', iver_header);
  init_table('#antonio-table', antonio_header);
  init_table('#kobe-table', kobe_header);
  init_table('#wb-table', wb_header);
  init_table('#harden-table', harden_header);
  init_table('#haward-table', haward_header);
  init_table('#oneal-table', oneal_header);
  init_table('#rockets-table', rockets_header);
  init_table('#rockets_week-table', rockets_week_header);
  init_table('#rockets_month-table', rockets_month_header);
  init_table('#james-table', james_header);
  init_table('#cpp-table', cpp_header);
  init_table('#cpp_month-table', cpp_month_header);
  init_table('#cppp_month-table', cppp_month_header);
  init_table('#cppp-table', cppp_header);
  init_table('#curry-table', curry_header);
  init_table('#curry_week-table', curry_week_header);
  init_table('#curry_month-table', curry_month_header);
  init_table('#lv-table', lv_header);
  init_table('#durant-table', durant_header);
  init_table('#yao-table', yao_header);
  init_table('#rondo-table', rondo_header);
  init_table('#rondop-table', rondop_header);

}

function init_table(id, header){
  var html_res = '<tr>'
  for(var i in header){
    html_res+='<td>'+header[i]+'</td>';
  }
  html_res+='</tr>';
  $(id).html(html_res);
}

function load_data(id, data){
  var str = "";
  for(var i in data){
    child = data[i];
    str += '<tr>';
    for(var j in child){
      if(j==0){
        str+='<td = class="td1">'+child[j]+'</td>';
      }
      else{
        str+='<td>'+child[j]+'</td>';
      }
    }
    str+='</tr>';
  }
  $(id).append(str);
}


function updateTable(data){
  var lines = data.split('\n');
  var info_str = '';
  var iver_data = [];
  var antonio_data = [];
  var kobe_data = [];
  var wb_data = [];
  var harden_data = [];
  var haward_data = [];
  var oneal_data = [];
  var rockets_data = [];
  var rockets_week_data = [];
  var rockets_month_data = [];
  var james_data = [];
  var cpp_data = [];
  var cpp_month_data = [];
  var cppp_month_data = [];
  var cppp_data = [];
  var lavine_data = [];
  var curry_data = [];
  var curry_week_data = [];
  var curry_month_data = [];
  var durant_data = [];
  var yao_data = [];
  var rondo_data = [];
  var rondop_data = [];
  for(var i=0;i<lines.length;i++){
    items = lines[i].split(',');
    last90 = items.pop();
    switch(items[0]){
      case 'num':
        $('#pre-num').text('请求数目：'+items[1]);
        break;
      case 'endtime':
        $('#pre-time').text('刷新时间：'+items[1]);
        break;
      case 'time':
        $('#pre-used').text('预测耗时'+items[1]);
        break;
      case 'iver':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4],
          items[5],
          items[6],
          items[7],
          items[8],
          items[9],
          items[10],
          items[11]
        ];
        iver_data.push(element);
        break;
      case 'antonio':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4],
          items[5],
          items[6],
          items[7],
          items[8],
          items[9]
        ];
        antonio_data.push(element);
        break;
      case 'kobe3':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4],
          items[5]
        ];
        kobe_data.push(element);
        break;
      case 'wb':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4],
          items[5]
        ];
        wb_data.push(element);
        break;
      case 'fl':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4],
          items[5],
          items[6],
          items[7],
          items[8],
          items[9],
          items[10]
        ];
        harden_data.push(element);
        break;
      case 'oneal':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4]
        ];
        oneal_data.push(element);
        break;
      case 'haward':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4]
        ];
        haward_data.push(element);
        break;
      case 'rockets':
        var element = [
          items[1],
          last90,
          items[2]
        ];
        rockets_data.push(element);
        break;
      case 'rockets_week':
        var element = [
          items[1],
          last90,
          items[2]
        ];
        rockets_week_data.push(element);
        break;
      case 'rockets_month':
        var element = [
          items[1],
          last90,
          items[2]
        ];
        rockets_month_data.push(element);
        break;
      case 'james':
        var element = [
          items[1],
          last90,
          items[2],
          items[3],
          items[4],
          items[5],
          items[6]
        ];
        james_data.push(element);
        break;
      case 'cpp':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        cpp_data.push(element);
        break;
      case 'cpp_month':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        cpp_month_data.push(element);
        break;
      case 'cppp_month':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        cppp_month_data.push(element);
        break;
      case 'cppp':
        var flag = '-';
        if(items[5]==1) flag = 'A';
        if(items[5]==2) flag = 'B';
        var element = [
          items[1],
          items[3],
          items[2],
          items[4],
          flag
        ];
        cppp_data.push(element);
        break;
      case 'curry':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        curry_data.push(element);
        break;
      case 'curry_week':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        curry_week_data.push(element);
        break;
      case 'curry_month':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        curry_month_data.push(element);
        break;
      case 'lavine':
        var element = [
          items[1],
          items[4],
          items[2],
          items[3],
          items[5]
        ];
        lavine_data.push(element);
        break;
      case 'durant':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        durant_data.push(element);
        break;
      case 'yao':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        yao_data.push(element);
        break;
      case 'rondo':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        rondo_data.push(element);
        break;
      case 'rondop':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        rondop_data.push(element);
        break;
      default:
        break;
    }
  }
  console.log(wb_data);
  wb_data.sort(compare_wb);
  load_data('#iver-table', iver_data);
  load_data('#antonio-table', antonio_data);
  load_data('#kobe-table', kobe_data);
  load_data('#wb-table', wb_data);
  load_data('#harden-table', harden_data);
  load_data('#haward-table', haward_data);
  load_data('#oneal-table', oneal_data);
  load_data('#rockets-table', rockets_data);
  load_data('#rockets_week-table', rockets_week_data);
  load_data('#rockets_month-table', rockets_month_data);
  load_data('#james-table', james_data);
  load_data('#cpp-table', cpp_data);
  load_data('#cpp_month-table', cpp_month_data);
  load_data('#cppp_month-table', cppp_month_data);
  load_data('#cppp-table', cppp_data);
  load_data('#curry-table', curry_data);
  load_data('#curry_week-table', curry_week_data);
  load_data('#curry_month-table', curry_month_data);
  load_data('#lv-table', lavine_data);
  load_data('#durant-table', durant_data);
  load_data('#yao-table', yao_data);
  load_data('#rondo-table', rondo_data);
  load_data('#rondop-table', rondop_data);
}

function compare_wb(a,b){
  if(a[5]>b[5]){
    return -1;
  }
  if(a[5]<b[5]){
    return 1;
  }
  return 0;
}

function hide_all(){
  document.getElementById('iver-div').style.display = "none";
  document.getElementById('antonio-div').style.display = "none";
  document.getElementById('kobe-div').style.display = "none";
  document.getElementById('oneal-div').style.display = "none";
  document.getElementById('wb-div').style.display = "none";
  document.getElementById('harden-div').style.display = "none";
  document.getElementById('rockets-div').style.display = "none";
  document.getElementById('rockets_week-div').style.display = "none";
  document.getElementById('rockets_month-div').style.display = "none";
  document.getElementById('james-div').style.display = "none";
  document.getElementById('cpp-div').style.display = "none";
  document.getElementById('cpp_month-div').style.display = "none";
  document.getElementById('cppp_month-div').style.display = "none";
  document.getElementById('cppp-div').style.display = "none";
  document.getElementById('curry-div').style.display = "none";
  document.getElementById('lv-div').style.display = "none";
  document.getElementById('durant-div').style.display = "none";
  document.getElementById('yao-div').style.display = "none";
  document.getElementById('rondo-div').style.display = "none";
  document.getElementById('rondop-div').style.display = "none";
  document.getElementById('curry_week-div').style.display = "none";
  document.getElementById('curry_month-div').style.display = "none";
  document.getElementById('haward-div').style.display = "none";
  document.getElementById('index-div').style.display = "none";
}

function classic_switch(){
  hide_all();
  document.getElementById('iver-div').style.display = "block";
  document.getElementById('antonio-div').style.display = "block";
  document.getElementById('kobe-div').style.display = "block";
  document.getElementById('oneal-div').style.display = "block";
  document.getElementById('wb-div').style.display = "block";
}

function new_switch(){
  hide_all();
  document.getElementById('harden-div').style.display = "block";
  document.getElementById('rockets-div').style.display = "block";
  document.getElementById('james-div').style.display = "block";
  document.getElementById('cpp-div').style.display = "block";
  document.getElementById('cppp-div').style.display = "block";
  document.getElementById('curry-div').style.display = "block";
  document.getElementById('lv-div').style.display = "block";
  document.getElementById('durant-div').style.display = "block";
  document.getElementById('haward-div').style.display = "block";
}

function week_switch(){
  hide_all();
  document.getElementById('yao-div').style.display = "block";
  document.getElementById('rondo-div').style.display = "block";
  document.getElementById('rondop-div').style.display = "block";
  document.getElementById('curry_week-div').style.display = "block";
  document.getElementById('rockets_week-div').style.display = "block";
}
function month_switch(){
  hide_all();
  document.getElementById('curry_month-div').style.display = "block";
  document.getElementById('rockets_month-div').style.display = "block";
  document.getElementById('cpp_month-div').style.display = "block";
  document.getElementById('cppp_month-div').style.display = "block";
}
function ai_switch(){
  hide_all();
}
function index_switch(){
  hide_all();
  document.getElementById('index-div').style.display = "block";
}
function load_index(){
  var index_header=[
    '开始日期',
    '结束日期',
    '最高价真实',
    '最高价预测',
    '最低价真实',
    '最低价预测',
    '收盘价真实',
    '收盘价预测'
  ]
  init_table('#index-table', index_header);
  /*iver_data.push(element);*/
  /*load_data('#iver-table', iver_data);*/
  $.ajax({
    url: '/index_pre',
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        data=data.replace(/\"/g,"");
        console.log(data);
        lines = data.split('\n');
        var index_data = [];
        for(var i in lines){
          items = lines[i].split(',');
          element = items.slice(1);
          index_data.push(element);
        }
        draw_index_pre(index_data);
        console.log(index_data)
        var new_data = [];
        for(var i=0;i<index_data[0].length;i++){
          new_data[i] = []
          for(var j=0;j<index_data.length-1;j++){
            new_data[i][j] = index_data[j][i];
          }
        }
        load_data('#index-table', new_data);
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}
function draw_index_pre(index_data){
  // 基于准备好的dom，初始化echarts实例
  var myChart = echarts.init(document.getElementById('chart-index'));
  var low_list = index_data[5];
  low_list = low_list.map(function(x){
    return parseFloat(x);
  })
  var min_val = Math.min.apply(Math, low_list);
  var max_val = Math.max.apply(Math, index_data[2]).toFixed(0);
  console.log(min_val);
  console.log(low_list);
  /*var date_list = index_data[0];*/
  var date_list = []
  for(var i=0;i<index_data[0].length;i++){
    date_list[i] = "";
    date_list[i] = index_data[0][i] +'~'+ index_data[1][i];
  }
  console.log(date_list)

  // 指定图表的配置项和数据
  var option = {
    title: {
      text: '大盘预测'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    toolbox: {
      feature: {
        dataView: {show: true, readOnly: false},
        magicType: {show: true, type: ['line', 'bar']},
        restore: {show: true},
        saveAsImage: {show: true}
      }
    },
    legend: {
      data:['最高价真实','最高价预测','最低价真实','最低价预测','收盘价真实','收盘价预测']
    },
    xAxis: [
      {
        type: 'category',
        data: date_list,
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '指数',
        min: (min_val*0.98).toFixed(0)
      }
    ],
    series: [
      {
        name:'最高价真实',
        type:'line',
        data:index_data[2]
      },
      {
        name:'最高价预测',
        type:'line',
        data:index_data[3]
      },
      {
        name:'最低价真实',
        type:'line',
        data:index_data[4]
      },
      {
        name:'最低价预测',
        type:'line',
        data:index_data[5]
      },
      {
        name:'收盘价真实',
        type:'line',
        data:index_data[6]
      },
      {
        name:'收盘价预测',
        type:'line',
        data:index_data[7]
      }
    ]
  };

  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
}
load_index();
classic_switch();
